package com.me.experiment;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.Pixmap;




/**
 * Graphical set containing sets of Pixmaps and how to draw them. 
 * @see Object
 */
public class Animation {
	String name_; // name of animation
	ArrayList<Frame> frames_; // list of frames
	float onsetTime_;
	boolean allatonce;
	
	public class Frame {
		Pixmap pixmap_;
		float delay_;
		float x_offset_;
		float y_offset_;
		int width_,height_; 

	}	
	
	/**
	 * Constructor
	 */
	public Animation() {
		frames_ = new ArrayList<Frame>();
		allatonce = false;
	}
	
	
	/**
	 * Returns the frame to display in the frame vector
	 * @param time
	 * @see Frame  
	 */
	public Frame getFrame(float time) {
			float totalTime = onsetTime_;
				for(Frame f : frames_) {
				if ((time>totalTime)&&(time<=totalTime + f.delay_)){
					return f;
				} 	
				totalTime += f.delay_;	
			}
			if (time>totalTime){
				 onsetTime_ = time; // to loop
			}
			
			return frames_.get(0);
	}

	/**
	 * Sets the onset time of an animation, the time when the animation is initiated
	 * @param time
	 */
	public void setOnsetTime(float time) {
		this.onsetTime_ = time;
	}

	
	/**
	 * adds a frame to the animation
	 * @param p Pixmap to add to the animation 
	 * @param d Time length of the Pixmap display in animation
	 */
	Frame addFrame(Pixmap p, float d) {
		frames_.add(new Frame());
		Frame f = frames_.get(frames_.size()-1);
		f.pixmap_ = p;
		f.delay_ = d;
		return f;
	}

	/**
	 * adds a frame to the animation
	 * @param p Pixmap to add to the animation 
	 * @param d Time length of the Pixmap display in animation
	 * @param x_offset
	 * @param y_offset
	 */
	Frame addFrame(Pixmap p, float x_offset, float y_offset) {
		frames_.add(new Frame());
		Frame f = frames_.get(frames_.size()-1);
		f.pixmap_ = p;
		f.width_ = p.getWidth();
		f.height_ = p.getHeight();
		f.x_offset_ = x_offset;
		f.y_offset_ = y_offset;
		return f;
	}
	
}
